// $codepro.audit.disable numericLiterals
/**
 * Class: Income Statement Panel
 * Description: 
 * 
 * 
 * 
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;



/**
 * 
 * @author Team Awesome Express
 * @version $ Revision 1.0 $
 *
 */
public class IncomeStatementPanel extends JPanel {

	/**
	 * DataManager myDataManager
	 */
	private DataManager myDataManager;

	/**
	 * Create the panel.
	 * @param myDataManager
	 */
	
	public IncomeStatementPanel(DataManager myDataManager) {
		setBackground(GlobalVars.BACKGROUND);
		this.myDataManager = myDataManager;
		setLayout(null);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(110, 5, 37, 20);
		add(comboBox);
		
		final JLabel lblMonth = new JLabel("Month");
		lblMonth.setBounds(69, 8, 46, 14);
		add(lblMonth);
		
		final JLabel lblResultsOutput = new JLabel("");
		lblResultsOutput.setBounds(10, 120, 400, 14);
		add(lblResultsOutput);
		
		comboBox.setMaximumRowCount(12);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", 
																 "2", 
																 "3", 
																 "4", 
																 "5", 
																 "6", 
																 "7", 
																 "8", 
																 "9", 
																 "10", 
																 "11", 
																 "12"}));
		comboBox.setToolTipText("Month");
		add(comboBox);
		
		final JButton btnSearch = new JButton("Calculate Monthly Income");
		btnSearch.setBounds(69, 48, 89, 23);
		btnSearch.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) { // If the Calculate Patients button is Pressed
				final String rawData = calculateIncomeStatement(comboBox.getSelectedIndex());
				final int totalMoney = Integer.parseInt(rawData.substring(0, rawData.length() - 1));
				final int numPatients = Integer.parseInt(rawData.substring(rawData.length() - 1, rawData.length()));
				lblResultsOutput.setText("The monthly income was: $" + 
										 totalMoney + 
										 ".00  after seeing " + 
										 numPatients + 
										 " patients."); 
			}
		});

		add(btnSearch);
		
	}

	/**
	 * 
	 * @param myDataManager
	 * @return frame
	 */
	public static JFrame createWindow(DataManager myDataManager) {
		final JFrame frame = new JFrame();
		frame.setSize(400, 220);
		frame.getContentPane().add(new IncomeStatementPanel(myDataManager));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		return frame;
		}

	/**
	 * 
	 * @param month
	 * @return totalMonthlyValue
	 */
	private String calculateIncomeStatement(int month) {
		
		final TreatmentRecord[] treatmentRecordsList = myDataManager.getTreatmentRecords(month + 1);
		int totalMonthlyValue = 0;
		for (int i=0; i < treatmentRecordsList.length;i++){
			totalMonthlyValue += treatmentRecordsList[i].calculateBillables();
		}
		
		return ((Integer) totalMonthlyValue).toString() + ((Integer) treatmentRecordsList.length).toString();
		
	}
}
